<template>
  <div class="main">
    <header>
      <div class="register-aliyun-topbar">
        <div class="aliyun-topbar-header">
          <div class="aliyun-topbar-element aliyun-logo">
            <a class="aliyun-logo-link">
              <img src="../../assets/logo.png" class="aliyun-logo-image" />
            </a>
            <span class="topbar-text">Login</span>
          </div>
        </div>
      </div>
    </header>
    <div class="ace-dpl-account">
      <div class="content">
        <a-form class="Loginform" :label-col="labelCol" :wrapper-col="wrapperCol">
          <div class="head">Engineering Service Request Platform</div>
          <!-- <a-alert
            v-if="isLoginError"
            type="error"
            showIcon
            style="margin-bottom: 24px;"
            message="账户或密码错误"
          />
          <a-alert
            v-if="!userName"
            type="error"
            showIcon
            style="margin-bottom: 24px;"
            message="请输入账号"
          />
          <a-alert
            v-if="!passWord"
            type="error"
            showIcon
            style="margin-bottom: 24px;"
            message="请输入密码"
          />-->
          <a-form-item label="NTID">
            <a-input size="large" type="text" placeholder v-model.trim="NTID" allowClear>
              <a-icon slot="prefix" type="user" :style="{ color: 'rgba(0,0,0,.25)' }" />
            </a-input>
          </a-form-item>

          <a-form-item label="PassWord">
            <a-input-password
              size="large"
              type="password"
              autocomplete="false"
              v-model.trim="passWord"
              allowClear
            >
              <a-icon slot="prefix" type="lock" :style="{ color: 'rgba(0,0,0,.25)' }" />
            </a-input-password>
          </a-form-item>
        </a-form>

        <div>
          <div class="fm-btn">
            <button
              type="submit"
              tabindex="3"
              class="fm-button fm-submit password-login"
              @click="submit"
            >Login</button>
          </div>
          <div class="user-login-other">
            <!-- <router-link class="register">Forget Password</router-link> -->
            <router-link class="register" :to="{ name: 'Register' }">Register</router-link>
          </div>
        </div>
      </div>
    </div>
  </div>
</template>

<script>
import api from '@/public/httpApi/user'
export default {
  name: 'Login',
  data() {
    return {
      labelCol: {
        xs: { span: 3 },
        sm: { span: 5 },
      },
      wrapperCol: {
        xs: { span: 10 },
        sm: { span: 19 },
      },
      isLoginError: false,
      NTID: '',
      passWord: '',
      defaultPassword: '',
    }
  },
  created() {
    document.onkeydown = (e) => {
      if (e.keyCode == 13) {
        this.submit()
      }
    }
  },
  beforeDestroy() {
    document.onkeydown = ''
  },
  methods: {
    submit() {
      if (!this.NTID) {
        this.$message.warn('Please enter the NTID')
        return
      } else if (!this.passWord) {
        this.$message.warn('Please enter the password')
        return
      } else {
        // 新增校验第一次登陆，强制通知修改密码
        api.getUserInfoById(this.NTID).then((result) => {
          console.log('当前用户 :>> ', result)
          if (result.data) {
            this.defaultPassword = result.data[0].DefaultPassword
            if (this.defaultPassword == this.passWord) {
              this.$message.warning(
                'You are using the default password, please change the password and log in. It will redirect you to the change password page'
              )
              setTimeout(() => {
                this.$router.push({ path: 'resetPassword' })
              }, 1500)
              return
            } else {
              const a = {
                method: 'SysUser_Login',
                userId: window.userId || 1,
                sign: '1',
                currentPage: 0,
                limit: 0,
                datas: {
                  NTID: this.NTID,
                  passWord: this.passWord,
                },
              }
              this.$post(a).then((result) => {
                console.log('result :>> ', result)
                if (result.code === 0) {
                  // 校验默认密码，提示用户修改密码再登陆
                  this.defaultPassword = result.data[0].DefaultPassword
                  if (this.defaultPassword == this.passWord) {
                    this.$message.warning(
                      'You are using the default password, please change the password and log in. It will redirect you to the change password page'
                    )
                    setTimeout(() => {
                      this.$router.push({ path: '/resetPassword' })
                    }, 1500)
                    return
                  }

                  sessionStorage.setItem('BackLoginStatus', true)
                  this.$message.success('Login successful!Jump to System soon')
                  setTimeout(() => {
                    this.$router.push({ path: '/' })
                  }, 500)
                }
                sessionStorage.setItem(
                  'BackUserInfo',
                  JSON.stringify(result.data)
                )
                console.log('登录信息', result.data)
                if (result.data && result.data.length > 0) {
                  window.userId = result.data[0].NTID
                  sessionStorage.setItem('userId', result.data[0].NTID)
                }
              })
            }
          }
        })
      }
    },
  },
}
</script>
<style lang="less" scoped>
@import url('css/login.less');
</style>
